package com.lvideo.component.extraplayer.videoview;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.MotionEvent;
import android.view.View;
import android.widget.MediaController;

import com.letv.sdk.callbacks.LetvPlayer;
import com.letv.sdk.callbacks.LetvPlayerError;
import com.letv.sdk.callbacks.LetvPlayerMonitor;
import com.letv.sdk.callbacks.LetvPlayerView;
import com.lvideo.component.extraplayer.Interface.MyAdStatusListener;
import com.lvideo.component.extraplayer.Interface.OnLetvSdkErrorListener;
import com.lvideo.component.extraplayer.LVideoMediaPlayerControl;
import com.media.ffmpeg.FFMpegPlayer;
import com.novaplayer.info.PlayUrl;
import com.novaplayer.listener.OnSubtitleListener;
import com.novaplayer.listener.OnVideoViewStateChangeListener;
import com.tencent.qqlive.mediaplayer.api.TVK_PlayerVideoInfo;
import com.tencent.qqlive.mediaplayer.api.TVK_UserInfo;

import java.util.List;
import java.util.Map;

/**
 * Created by biandi on 2017/3/10.
 */

public class VideoViewLetvProxy implements LVideoMediaPlayerControl {
    private Context mContext;
    private LetvPlayer mLetvPlayer;
    private LetvPlayerView mVideoView;
    private Long mVid;

    private MediaPlayer.OnPreparedListener mOnPreparedListener;
    private MediaPlayer.OnCompletionListener mOnCompletionListener;
    private OnLetvSdkErrorListener mOnLetvErrorListenter;
    private MediaPlayer.OnInfoListener mOnInfoListener;
    private MediaPlayer.OnSeekCompleteListener mOnSeekCompleteListener;

    private static final int UNKNOWN_ERROR_CODE = 100;
    private static final int INTERNAL_ERROR_CODE = UNKNOWN_ERROR_CODE + 1;
    private static final int TIMEOUT_ERROR_CODE = INTERNAL_ERROR_CODE + 1;
    private static final int NON_COPYRIGHT_ERROR_CODE = TIMEOUT_ERROR_CODE + 1;
    private static final int NOSURPORT_ERROR_CODE = NON_COPYRIGHT_ERROR_CODE + 1;
    private static final int NETWORK_ERROR_CODE = NOSURPORT_ERROR_CODE + 1;
    private static final int DATA_GET_FAILED_ERROR_CODE = NETWORK_ERROR_CODE + 1;
    private static final int EXCEPT_SEVEN_ERROR_CODE = DATA_GET_FAILED_ERROR_CODE + 1; // 除了7个错误码以外的错误
    private static final int NULL_INFO_ERROR_CODE = EXCEPT_SEVEN_ERROR_CODE + 1; // errorinfo为空的情况
    private int firstErrorCode = 0;

    public VideoViewLetvProxy(Context context) {
        this.mContext = context;
        mLetvPlayer = LetvPlayer.getInstance(context);
        mLetvPlayer.setLetvPlayerMonitor(mLetvPlayerMonitor);
        mLetvPlayer.onCreate();

    }

    private final LetvPlayerMonitor mLetvPlayerMonitor = new LetvPlayerMonitor() {

        @Override
        public void onPrepared() {
            if (mOnPreparedListener != null) {
                mOnPreparedListener.onPrepared(null);
            }
        }

        @Override
        public void onPlay() {

        }

        @Override
        public void onPause() {

        }

        @Override
        public void onStop() {

        }

        @Override
        public void onError(LetvPlayerError letvPlayerError) {

        }

        @Override
        public void onError(String s) {
            if (mOnLetvErrorListenter != null) {
                mOnLetvErrorListenter.onLetvSdkError(null,s,0);
            }
        }


        @Override
        public void onBlock(int i) {

        }

        @Override
        public void onStartLoading() {

        }

        @Override
        public void onLoadSuccess() {

        }

        @Override
        public void onSeekComplete() {
            if (mOnSeekCompleteListener != null) {
                mOnSeekCompleteListener.onSeekComplete(null);
            }
        }

        @Override
        public void updateProgress(int i, int i1) {

        }

        @Override
        public void initProcess(int i, int i1, int i2) {

        }

        @Override
        public void onComplete() {
            mOnCompletionListener.onCompletion(null);
        }

        @Override
        public void onPlayToTail() {

        }


        @Override
        public void onDefinitionChanged(boolean b) {

        }

        @Override
        public void onADBlock(boolean b) {

        }

        @Override
        public void onADPlay() {

        }

        @Override
        public void onADFinish() {

        }

        @Override
        public void onADLoading() {

        }

        @Override
        public void onADPaused() {

        }

        @Override
        public void onDownloadSuccess(String s) {

        }

        @Override
        public void onDownloadFail(LetvPlayerError letvPlayerError) {

        }

        @Override
        public void onVideoBufferingStart() {
            if(mOnInfoListener != null){
                mOnInfoListener.onInfo(null, MediaPlayer.MEDIA_INFO_BUFFERING_START, 0);
            }
        }

        @Override
        public void onVideoBufferingEnd() {
            if(mOnInfoListener != null){
                mOnInfoListener.onInfo(null, MediaPlayer.MEDIA_INFO_BUFFERING_END, 0);
            }
        }
    };

    @Override
    public void setVideoPlayUrl(PlayUrl url) {

    }

    @Override
    public void setVideoPath(String videoPath) {

    }

    @Override
    public void setVideoPath(String videoPath, Map<String, String> headers) {

    }

    @Override
    public void setOnAdListener(MyAdStatusListener var) {

    }

    @Override
    public void stopPlayback() {
        mLetvPlayer.onStop();
        mLetvPlayer.onDestroy();
    }

    @Override
    public void forward() {

    }

    @Override
    public void rewind() {

    }

    @Override
    public void adjust(int type) {

    }

    @Override
    public MediaPlayer getMediaPlayer() {
        return null;
    }

    @Override
    public View getView() {
        return null;
    }

    @Override
    public int getLastSeekWhenDestoryed() {
        return 0;
    }

    @Override
    public boolean isPaused() {
        return mVideoView.isPaused();
    }

    @Override
    public boolean isInPlaybackState() {
        return mLetvPlayer.isPlaybackState();
    }

    @Override
    public void setMediaController(MediaController controller) {

    }

    @Override
    public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {
        this.mOnPreparedListener = l;
    }

    @Override
    public void setOnCompletionListener(MediaPlayer.OnCompletionListener l) {
        this.mOnCompletionListener = l;
    }

    @Override
    public void setOnInfoListener(MediaPlayer.OnInfoListener l) {
        this.mOnInfoListener = l;
    }

    @Override
    public void setOnErrorListener(MediaPlayer.OnErrorListener l) {

    }

    public void setOnLetvErrorListener(OnLetvSdkErrorListener l) {
        this.mOnLetvErrorListenter = l;
    }

    @Override
    public void setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener l) {
        this.mOnSeekCompleteListener = l;
    }

    @Override
    public void setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener l) {

    }

    @Override
    public void setOnBufferingUpdateListener(MediaPlayer.OnBufferingUpdateListener l) {

    }

    @Override
    public void setVideoViewStateChangeListener(OnVideoViewStateChangeListener videoViewStateChangeListener) {

    }

    @Override
    public void setOnSubtitleListener(OnSubtitleListener onSubtitleListener) {

    }

    @Override
    public void setInitPosition(int i) {

    }

    @Override
    public void switchStreamSource(String s, int i, int i1) {

    }

    @Override
    public int setSourceType(int i) {
        return 0;
    }

    @Override
    public void setParameter(int i, Object o) {

    }

    @Override
    public boolean handleTouchEvent(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void usingSensor(int i, boolean b) {

    }

    @Override
    public void setLanguage(String s) {

    }

    @Override
    public String[] getLanguage() {
        return new String[0];
    }

    @Override
    public void setOnAdNumberListener(FFMpegPlayer.OnAdNumberListener l) {

    }

    @Override
    public void setOnBlockListener(FFMpegPlayer.OnBlockListener l) {

    }

    @Override
    public void setOnHardDecodeErrorListener(FFMpegPlayer.OnHardDecodeErrorListner l) {

    }

    @Override
    public boolean isEnforcementWait() {
        return false;
    }

    @Override
    public void setEnforcementWait(boolean enforcementWait) {

    }

    @Override
    public boolean isEnforcementPause() {
        return false;
    }

    @Override
    public void setEnforcementPause(boolean enforcementPause) {

    }

    @Override
    public void setCacheSize(int video_size, int audio_size, int picutureSize, int startpic_size) {

    }

    @Override
    public boolean isSohuAdvertInPlayback() {
        return mLetvPlayer.isAdvertInPlayback();
    }

    @Override
    public void next() {

    }

    @Override
    public void playVideoAt(int index) {

    }

    @Override
    public List<Integer> getSupportDefinitions() {
        return mLetvPlayer.getSupportDefinitions();
    }

    @Override
    public void changeDefinition(int definition) {
        mLetvPlayer.changeDefinition(definition);
    }

    @Override
    public int getCurrentDefinition() {
        return mLetvPlayer.getCurrentDefinition();
    }

    @Override
    public void setTencentData(TVK_UserInfo userinfo, TVK_PlayerVideoInfo playerinfo, int currPlayTime) {

    }

    @Override
    public void setLetvScreenView(LetvPlayerView letvScreenView) {
        mVideoView = letvScreenView;
        mLetvPlayer.setLetvScreenView(mVideoView);
    }

    @Override
    public void setLetvData(long vid,int currPlayTime) {
        mVid = vid;
        mLetvPlayer.playVideo(vid, 0, currPlayTime, "");
    }

    @Override
    public void setBestvData(String vid, String fdnCode, long currPlayTime) {

    }

    @Override
    public int getFirstErrorCode() {
        return firstErrorCode;
    }

    @Override
    public void start() {
        mLetvPlayer.play();
    }

    @Override
    public void pause() {
        mLetvPlayer.pause();
    }

    @Override
    public int getDuration() {
        return (int) mLetvPlayer.getDuration();
    }

    @Override
    public int getCurrentPosition() {
        return (int) mLetvPlayer.getCurrentPosition();
    }

    @Override
    public void seekTo(int i) {
        mLetvPlayer.mPlayerView.seekTo(i, false);
    }

    @Override
    public boolean isPlaying() {
        return mVideoView.isPlaying();
    }

    @Override
    public int getBufferPercentage() {
        return 0;
    }

    @Override
    public boolean canPause() {
        return true;
    }

    @Override
    public boolean canSeekBackward() {
        return false;
    }

    @Override
    public boolean canSeekForward() {
        return false;
    }

    @Override
    public int getAudioSessionId() {
        return 0;
    }

    @Override
    public void onDestroyData() {
        try {
            if (mLetvPlayer != null) {
                mLetvPlayer.setLetvScreenView(null);
                mLetvPlayer.onDestroy();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        mVideoView = null;
        mLetvPlayer = null;
    }
}
